/**
* 产权
*/
<template>
    <el-form v-loading="loading" :model="formData" :rules="isEditPage ? formRules : {}" ref="formRef"
        label-width="120px" :scroll-to-error="true">
        <vp-card>
            <el-row style="flex: 1;">
                <vp-col :span="1">
                    <el-form-item prop="bh">
                        <template #label><vp-label zh="编号" en="" align="left" /></template>
                        <el-input v-model="formData.bh" v-trim maxlength="20" v-if="type === 'add'"></el-input>
                        <span v-else>{{ formData.bh }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1">
                    <el-form-item prop="mc">
                        <template #label><vp-label zh="名称" en="" align="left" /></template>
                        <el-input v-model="formData.mc" maxlength="50" v-if="isEditPage"></el-input>
                        <span v-else>{{ formData.mc }}</span>
                    </el-form-item>
                </vp-col>
            </el-row>
        </vp-card>

    </el-form>
</template>
<script setup lang="ts" name="Ata">
import { onMounted, ref, computed } from "vue";
import request from "/@/utils/request";
import validateUtils from "/@/utils/validateUtils";

const emits = defineEmits(['formSubmited']);

const formData = ref({
    id: "",
    bh: "",
    mc: "",
});
const formRules = {
    bh: [validateUtils.notNull],
    mc: [validateUtils.notNull]
}

const initFormData = () => {
    formData.value = {
        id: "",
        bh: "",
        mc: "",
    }
}

const type = ref('add');
const loading = ref(false);
const formRef = ref();

const props = defineProps<{
    id: string,
    type: string,
}>()

onMounted(() => {
    handleOpen(props.type, props.id);
})

// 是否是编辑页面
const isEditPage = computed(() => {
    return props.type === 'add' || props.type === 'edit';
});

const handleOpen = (_type: string, id: string) => {
    type.value = _type;
    initFormData();
    if (type.value != "add") {
        loading.value = true;
        request.get(`/basicData/propertyRights/${id}`).then((res) => {
            formData.value = {
                ...formData.value,
                ...res.data,
            };
            loading.value = false;
            emits("formSubmited", formData.value);
        });
    }
}

const getData = (zt: number): Promise<any> => {
    let promise = new Promise((resolve, reject) => {
        formRef.value.validate((valid: boolean) => {
            if (valid) {
                resolve({
                    ...formData.value,
                });
            } else {
                reject();
            }
        })
    })
    return promise;
};


defineExpose({
    getData
})

</script>
<style scoped>
:deep(.row-warning1) {
    color: var(--el-color-danger);
}
</style>